var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/Select/SelectWithOther/index.tsx
var SelectWithOther_exports = {};
__export(SelectWithOther_exports, {
  default: () => SelectWithOther_default
});
module.exports = __toCommonJS(SelectWithOther_exports);
var import_antd = require("antd");
var import_style = require("antd/es/select/style/index.js");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_createField = __toESM(require("../../createField"));
var import_FieldContext = require("../../_utils/contexts/FieldContext");
var import_useOther = __toESM(require("../../_utils/hooks/useOther"));
var import_useRequestOptions = __toESM(require("../../_utils/hooks/useRequestOptions"));
var import_index = require("./index.less");
var SelectWithOther = ({
  value,
  onChange,
  options: optionsOrRequest,
  className,
  ...rest
}) => {
  const { label } = (0, import_FieldContext.useFieldContext)();
  const { options } = (0, import_useRequestOptions.default)(optionsOrRequest);
  const otherRef = (0, import_react.useRef)("select-other-" + Math.random());
  const {
    otherChecked,
    changeOtherChecked,
    selectedValue,
    changeSelectedValue,
    otherValue,
    changeOtherValue
  } = (0, import_useOther.default)({
    mode: "single",
    value,
    onChange,
    options
  });
  const handleSelectChange = (0, import_react.useCallback)(
    (val) => {
      const found = options.find((option) => option.value === val);
      if (found) {
        changeOtherChecked(false);
        changeSelectedValue(found.value);
      } else {
        changeOtherChecked(true);
      }
    },
    [options, changeSelectedValue, changeOtherChecked]
  );
  const handleInputChange = (0, import_react.useCallback)(
    (event) => {
      changeOtherValue(event.target.value);
    },
    [changeOtherValue]
  );
  return /* @__PURE__ */ import_react.default.createElement("span", { className: (0, import_classnames.default)("szzj-select-with-other", className) }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Select,
    {
      value: otherChecked ? otherRef.current : selectedValue == null ? void 0 : selectedValue[0],
      onChange: handleSelectChange,
      placeholder: `请选择${label}`,
      ...rest,
      className: "szzj-select-with-other-select"
    },
    (options || []).map((option) => {
      return /* @__PURE__ */ import_react.default.createElement(import_antd.Select.Option, { key: option.value, value: option.value }, option.label);
    }),
    /* @__PURE__ */ import_react.default.createElement(import_antd.Select.Option, { value: otherRef.current }, "其他")
  ), otherChecked ? /* @__PURE__ */ import_react.default.createElement(
    import_antd.Input,
    {
      value: otherChecked ? otherValue : void 0,
      onChange: handleInputChange,
      maxLength: 100,
      className: "szzj-select-with-other-input"
    }
  ) : null);
};
var SelectWithOther_default = (0, import_createField.default)(
  SelectWithOther
);
